<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2021-12-09 22:00:52
 * @LastEditors: Sam
 * @LastEditTime: 2022-10-18 18:19:09
 */
namespace app\api\controller;
use think\facade\Db;
use app\__MODULE__\model\{Order,StoreCommission,UserCommission,Distributor,CustomerCommission,Store};

/**
 * NotParse
 */
class Task {
    

    /**
     * 
     * @return void
     * @author Sam
     *
     */
    public  function check_order_expire(){
        $list = Order::where(['paid'=>0])->field('id,paid,expire_time')->select();
        if(count($list)){
            foreach ($list as $key => $value) {
                if(time() >= $value->expire_time){
                    $value->is_del = 1;
                    $value->save();
                }
            }
        } 
        \json_response(1,'success');
    }

    /**
     * 订单自动收货
     * @return void
     * @author Sam
     * @date 2022-01-28
     */
    public function auto_take(){
        $list = Order::where(['paid'=>1,'status'=>1,'refund_status'=>0])->field('id,pay_time,delivery_time')->select();
        if(count($list)){
            foreach ($list as $key => $value) {
                $expire_day = (time() - $value->delivery_time) / (1*24*60*60);
                if($expire_day >= 7){
                    //发货超过七天
                    $value->status = 2;
                    $value->take_time = time();
                    $value->save();
                }
            }
            json_response(1,'success');
        }
    }

    /**
     * 佣金订单确认
     * @return void
     * @author Sam
     * @date 2022-01-28
     */
    public function auto_commission(){
        $list = Order::where(['paid'=>1,'refund_status'=>0])->where('status','in',[2,3])->field('id,pay_time,take_time')->select();
    
        if(count($list)){
            foreach ($list as $key => $value) {
                $expire_day = (time() - $value->take_time) / (1*24*60*60);
                if($expire_day >=7){
                    //收货七天后
                  $user_commission =   UserCommission::where(['oid'=>$value->id,'status'=>-1])->find(); //核销员佣金
                  if($user_commission){
                      $user_commission->status = 0;
                      $user_commission->save();
                      Distributor::income($user_commission->uid,$user_commission->user_commission);
                  }
                }
            }
            
        }
        json_response(1,'success');
    }
}

